﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace WellDone
{
    public sealed partial class MainPage : Page
    {
        DispatcherTimer dtCheckForOrders;
        double dblOrderWidth, dblOrderHeight;
        public MainPage()
        {
            this.InitializeComponent();
            // Load the initial set of orders
            //checkForOrders(); 
            // Setup a timer to check for new sent orders on a periodic basis...
            dtCheckForOrders = new DispatcherTimer();
            dtCheckForOrders.Tick += (s, e) => { checkForOrders(); };
            dtCheckForOrders.Interval = new TimeSpan(0, 0, 1);
            dtCheckForOrders.Start();
            //dblOrderWidth = (Windows.UI.Xaml.Window.Current.Bounds.Width / Globals.k_int_num_horizontal_orders) - (Globals.k_dbl_margin_size * 2);
            //dblOrderHeight = (Windows.UI.Xaml.Window.Current.Bounds.Height / 2) - (Globals.k_dbl_margin_size * 2);
            dblOrderHeight = Windows.UI.Xaml.Window.Current.Bounds.Height - (Globals.k_dbl_margin_size * 2);
            // Go back to the login screen if the current screen looses visibility
/*
            Window.Current.VisibilityChanged += (s, e) =>
            {
                if (!e.Visible)
                {
                    // Don't bother checking for orders if the screen is not visible...
                    dtCheckForOrders.Stop();
                    // Return to the login screen...
                    Frame rootFrame = Window.Current.Content as Frame;
                    rootFrame.Navigate(typeof(Login), e);
                }
            };
 */
        }
        private async void checkForOrders()
        {
            if (await Globals.loadSentOrders())
            {
                // Traverse over the open orders...
                foreach (ServiceReferenceOpenRPOS.OrderSummary os in Globals.OrderSummaryRecords)
                {
                    ViewOrderControl voc = new ViewOrderControl();
                    //voc.Width = dblOrderWidth;
                    voc.Width = 350;
                    voc.Height = dblOrderHeight;
                    voc.setOrderNumber(os.col_ID % 100);
                    foreach (ServiceReferenceOpenRPOS.OrderDetail od in Globals.OrderDetailRecords.Where(x => x.col_OrderSummaryID == os.col_ID))
                    {
                        // Update the UI...
                        try
                        {
                            voc.addItemToList(Globals.MenuItemRecords.Where(x => x.col_ID == od.col_MenuItemID).First().col_FullName);
                            // Load options...
                            foreach (ServiceReferenceOpenRPOS.OrderOption oo in Globals.OrderOptionRecords.Where(x => x.col_OrderDetailID == od.col_ID))
                            {
                                // Display the option...
                                voc.addItemToList("  "+Globals.MenuItemRecords.Where(x => x.col_ID == oo.col_MenuItemID).First().col_FullName);
                            }
                        }
                        catch (Exception) { }
                    }
                    gvMain.Items.Add(voc);
                }
            }
        }
    }
}
